package com.hoolai.db;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.github.pagehelper.PageHelper;

/**
 * @author liyuwei
 * DataSource is defined in class DynamicDataSource
 */
@Configuration
@ComponentScan
@MapperScan("com.hoolai")
public class DatabaseConfig {

	@Autowired
	private DataSource dataSource;

	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);

		// 分页插件
		PageHelper pageHelper = new PageHelper();
		Properties props = new Properties();
		props.setProperty("reasonable", "true");
		props.setProperty("supportMethodsArguments", "true");
		props.setProperty("returnPageInfo", "check");
		props.setProperty("params", "count=countSql");
		pageHelper.setProperties(props);
		// 添加插件
		sqlSessionFactoryBean.setPlugins(new Interceptor[] { pageHelper });

		org.apache.ibatis.session.Configuration mybatisConfig = new org.apache.ibatis.session.Configuration();

		mybatisConfig.setMapUnderscoreToCamelCase(true);

		// mybatisConfig.addInterceptor(dbInterceptor);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

		SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
		sqlSessionFactoryBuilder.build(mybatisConfig);

		sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/hoolai/mybatis/*.xml"));
		sqlSessionFactoryBean.setSqlSessionFactoryBuilder(sqlSessionFactoryBuilder);
		sqlSessionFactoryBean.setConfiguration(mybatisConfig);
		return sqlSessionFactoryBean.getObject();
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	/**
	 * when reach the request pointcut, should read request value of param
	 * "dbKey", and set the key into databaseKeyHolder,<br/>
	 * RoutingDataSource will use this key later in the
	 * determineCurrentLookupKey() method.
	 * 
	 * @return
	 */
	@Bean
	@Scope(value = "thread", proxyMode = ScopedProxyMode.TARGET_CLASS)
	public CustomerContextHolder databaseKeyHolder() {
		return new CustomerContextHolder();
	}

}
